#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{   
    int sockfd = -1, acceptfd = -1;
    struct sockaddr_in servaddr;
    socklen_t addrlen = sizeof(servaddr);

    sockfd = socket(AF_INET, SOCK_STREAM, 0); //创建网络套接字
    if (sockfd < 0){
        printf("socket failed\n");
        return -1;
    }

    memset(&servaddr, 0, addrlen);
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //将点分十进制的ip地址转化成网络字节序的无符号的32位整形数据
    servaddr.sin_port = htons(8888); //将主机字节序转化成网络字节序

    if(connect(sockfd, (struct sockaddr*)&servaddr, addrlen) < 0){ //给服务器发送连接的请求
        printf("connect failed\n");
        return -1;
    }
    int num = 0;
    while(1){
        if (num > 4){
            break;
        }
        send(sockfd, "hello world!", sizeof("hello world!"), 0); //发送hello world!到socket
        num++;
        sleep(1);
    }
    close(sockfd);
    return 0;
}
